package com.hunktimes.wholesale.ui.pages

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BasePageFragment
import com.hunktimes.wholesale.databinding.FragmentForgetPwdBinding
import com.hunktimes.wholesale.extension.isHKPhone
import com.hunktimes.wholesale.util.ToastUtil
import com.hunktimes.wholesale.vm.ForgetPwdVM
import com.hunktimes.wholesale.widget.CountDown

/**
 * Author: Delete
 * Date: 2020/7/29 10:23
 * Description:
 *
 */
class ForgetPwdFragment : BasePageFragment(R.layout.fragment_forget_pwd) {
    override val titleRes: Int
        get() = R.string.title_forget_pwd
    lateinit var viewBinding: FragmentForgetPwdBinding
    val vm by viewModels<ForgetPwdVM>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentForgetPwdBinding.bind(view)
        viewBinding.titleBar.opTv.text = "保存"
    }
    var countDown: CountDown? = null
    override fun onStart() {
        super.onStart()
        viewBinding.phoneEt.doOnTextChanged { text, _, _, _ -> vm.phone.value = text.toString() }
        viewBinding.pwdEt.doOnTextChanged { text, _, _, _ -> vm.pwd.value = text.toString() }
        viewBinding.pwdAgainEt.doOnTextChanged { text, _, _, _ ->
            vm.pwdAgain.value = text.toString()
        }

        vm.pwdDlc.observe(this, Observer {

        })
        viewBinding.titleBar.opTv.setOnClickListener {
            val phone = vm.phone.value ?: ""
            val pwd = vm.pwd.value ?: ""
            val pwdAgain = vm.pwdAgain.value ?: ""
            if (phone.isBlank()) {
                ToastUtil.showErrorToast("请输入电话号码")
            } else if (!phone.isHKPhone()) {
                ToastUtil.showErrorToast("请输入正确的电话号码")
            } else if (pwd.isBlank()) {
                ToastUtil.showErrorToast("请输入新的密码")
            } else if (pwdAgain.isBlank()) {
                ToastUtil.showErrorToast("请确认密码")
            } else if (!TextUtils.equals(pwd, pwdAgain)) {
                ToastUtil.showErrorToast("两次密码输入不一致")
            }else{
                vm.forgetPwd()
            }
        }

        viewBinding.sendSmsTv.setOnClickListener {
            val phone = vm.phone.value ?: ""
            if (phone.isBlank()) {
                ToastUtil.showErrorToast("请输入电话号码")
            } else if (!phone.isHKPhone()) {
                ToastUtil.showErrorToast("请输入正确的电话号码")
            }else{
                vm.sendSmsCode()
                countDown = CountDown(requireContext(), it as TextView, 10)
                //lifecycle.addObserver(countDown!!)
                countDown?.start()
            }
        }

    }

    override fun onDestroy() {
        countDown?.cancelWrap()
        super.onDestroy()
    }
}